export default {
  data() {
    return {
      showId: 0, //表格id
    };
  },
  methods: {
    hasOne(value) {
      let itemRepeat;
      value.some((item, index) => {
        const names = this.multipleSelection.map((item1) => item1[item]);
        const nameSet = new Set(names);
        if (nameSet.size !== names.length) {
          itemRepeat = item;
          return true;
        }
      });
      return { itemRepeat };
    },
    tableRuler(value) {
      let indexEmpty;
      let itemEmpty;
      if (this.multipleSelection.length) {
        this.multipleSelection.some((item, index) => {
          return value.some((item1) => {
            if (!item[item1]) {
              indexEmpty = index;
              itemEmpty = item1;
              return true;
            }
          });
        });
      }
      return { indexEmpty, itemEmpty };
    },
    addRulesMixin(arr, callback) {
      const { formRules } = this;
      arr.forEach((item) => {
        this.$set(formRules, item, [{ required: true, message: "请输入", trigger: "change" }]);
      });
      callback && callback();
    },
    deletParam(data) {
      const { PROPS } = this;
      delete data[PROPS.createPeople];
      delete data[PROPS.createdAt];
      delete data[PROPS.LastUpdataPeople];
      delete data[PROPS.LastUpdataTime];
    },
    initDrawMixin() {
      const { detail } = this;
      Object.keys(detail).forEach((item) => {
        this.$set(detail, item, "");
      });
      if (this.showId) this.showId = 0;
    },
    onRowCopyAdd(row = {}, ref = "") {
      this.showId++;
      const data = { ...row };
      this.filterEmpty &&
        this.filterEmpty.forEach((item) => {
          data[item] = "";
        });
      data.showId = this.showId;
      this.dataList.push(data);
      this.$refs.tableRef.toggleRowSelection(data);
    },
    async onDel(row, field = "") {
      try {
        if (row.length) {
          await this.$confirm(`确定删除${field ? `「${row[0][field]}」` : ""}吗？`, "确认信息", {
            type: "warning",
            cancelButtonClass: "common-confirm-cancel-btn",
            confirmButtonClass: "common-confirm-confirm-btn",
          });
          this.delMultipleSelection(row, "showId");
          this.dataList = this.dataList.filter((item1) => {
            return row.every((item2) => {
              return item2.showId !== item1.showId;
            });
          });
          this.$message.success("删除成功");
        } else {
          this.$message.warning("请先勾选");
        }
      } catch {
        this.$message.warning("已取消删除");
      }
    },
    toggleSelection() {
      this.$refs.tableRef.clearSelection();
    },
    delMultipleSelection(value, showId = "id") {
      this.multipleSelection = this.multipleSelection.filter((item) => {
        return value.some((item1) => item[showId] !== item1[showId]);
      });
      value.forEach((row) => {
        this.$refs.tableRef.toggleRowSelection(row, false);
      });
      if (!this.multipleSelection.length) {
        this.toggleSelection();
      }
    },
  },
};
